/*
 * ConfigurationFrame.java
 *
 * Created on 15 de Dezembro de 2008, 22:08
 */

package br.com.kibutx.bixochecker.gui;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import net.sourceforge.napkinlaf.NapkinLookAndFeel;
import br.com.kibutx.bixochecker.config.ConfigurationManager;
import br.com.kibutx.bixochecker.config.ProcessorsConfigManager;
import br.com.kibutx.bixochecker.gui.config.InvalidConfigException;

/**
 * 
 * @author moa
 */
public class ConfigurationFrame extends javax.swing.JFrame {

	private static final long serialVersionUID = -958815760610826844L;
	private static final String GENERALTAB_TITLE = "Geral";
	private static final String CONNECTIONTAB_TITLE = "Conexão";
	private static final String CHECKERTAB_TITLE = "Vestibulares";
	private static final String CONFIG_TITLE = "BixoChecker Config";

	private ConfigGeneralTab generalTab;
	private ConfigConnectionTab connectionTab;
	private CheckerTab checkerTab;
	private ConfigurationManager configurationManager;
	private ProcessorsConfigManager processorsConfigManager;

	/** Creates new form ConfigurationFrame */
	public ConfigurationFrame(ConfigurationManager configurationManager, ProcessorsConfigManager processorsConfigManager) {
		initComponents();
		initTabs();
		this.configurationManager = configurationManager;
		this.processorsConfigManager = processorsConfigManager;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated
	// <editor-fold defaultstate="collapsed" desc="Generated
	// Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		tabbedPane = new javax.swing.JTabbedPane();
		pnlButton = new javax.swing.JPanel();
		saveBtn = new javax.swing.JButton();
		cancelBtn = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle(CONFIG_TITLE);
		setResizable(false);

		pnlButton.setBorder(javax.swing.BorderFactory
				.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

		saveBtn.setText("Salvar");
		saveBtn.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				saveBtnActionPerformed(evt);
			}
		});

		cancelBtn.setText("Cancelar");
		cancelBtn.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cancelBtnActionPerformed(evt);
			}
		});

		org.jdesktop.layout.GroupLayout pnlButtonLayout = new org.jdesktop.layout.GroupLayout(
				pnlButton);
		pnlButton.setLayout(pnlButtonLayout);
		pnlButtonLayout.setHorizontalGroup(pnlButtonLayout.createParallelGroup(
				org.jdesktop.layout.GroupLayout.LEADING).add(
				pnlButtonLayout.createSequentialGroup().addContainerGap().add(
						saveBtn,
						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 105,
						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(
								org.jdesktop.layout.LayoutStyle.RELATED, 99,
								Short.MAX_VALUE).add(cancelBtn,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
								101,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.addContainerGap()));
		pnlButtonLayout.setVerticalGroup(pnlButtonLayout.createParallelGroup(
				org.jdesktop.layout.GroupLayout.LEADING).add(
				org.jdesktop.layout.GroupLayout.TRAILING,
				pnlButtonLayout.createSequentialGroup().addContainerGap().add(
						pnlButtonLayout.createParallelGroup(
								org.jdesktop.layout.GroupLayout.TRAILING).add(
								org.jdesktop.layout.GroupLayout.LEADING,
								cancelBtn,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE).add(
								org.jdesktop.layout.GroupLayout.LEADING,
								saveBtn,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)).addContainerGap()));

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				org.jdesktop.layout.GroupLayout.LEADING).add(
				org.jdesktop.layout.GroupLayout.TRAILING,
				layout.createSequentialGroup().addContainerGap().add(
						layout.createParallelGroup(
								org.jdesktop.layout.GroupLayout.TRAILING).add(
								org.jdesktop.layout.GroupLayout.LEADING,
								tabbedPane,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								333, Short.MAX_VALUE).add(
								org.jdesktop.layout.GroupLayout.LEADING,
								pnlButton,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)).addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(
				org.jdesktop.layout.GroupLayout.LEADING).add(
				org.jdesktop.layout.GroupLayout.TRAILING,
				layout.createSequentialGroup().addContainerGap().add(
						tabbedPane,
						org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 291,
						Short.MAX_VALUE).addPreferredGap(
						org.jdesktop.layout.LayoutStyle.RELATED).add(pnlButton,
						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
						org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.addContainerGap()));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_cancelBtnActionPerformed
		this.setVisible(false);
		this.dispose();
	}// GEN-LAST:event_cancelBtnActionPerformed

	private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_saveBtnActionPerformed
		saveConfig(true);
	}// GEN-LAST:event_saveBtnActionPerformed

	public void initTabs() {
		generalTab = new ConfigGeneralTab();
		connectionTab = new ConfigConnectionTab();
		checkerTab = new CheckerTab();
		tabbedPane.add(GENERALTAB_TITLE, generalTab);
		tabbedPane.add(CONNECTIONTAB_TITLE, connectionTab);
		tabbedPane.add(CHECKERTAB_TITLE,checkerTab);
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton cancelBtn;
	private javax.swing.JPanel pnlButton;
	private javax.swing.JButton saveBtn;
	private javax.swing.JTabbedPane tabbedPane;

	// End of variables declaration//GEN-END:variables

	public ConfigurationManager getConfigurationManager() {
		return configurationManager;
	}

	public void loadConfig() {
		generalTab.load(configurationManager);
		connectionTab.load(configurationManager);
		checkerTab.load(this.processorsConfigManager);
	}

	public void saveConfig(boolean closeIfSuccessfull) {
		try {
			generalTab.save(configurationManager);
			connectionTab.save(configurationManager);
			checkerTab.save(this.processorsConfigManager);
			configurationManager.persist();
			processorsConfigManager.persist();
			JOptionPane.showMessageDialog(this, "Configurações Salvas!",
					"Configurações", JOptionPane.INFORMATION_MESSAGE);
			if (closeIfSuccessfull) {
				this.setVisible(false);
				this.dispose();
			}
		} catch (InvalidConfigException e) {
			JOptionPane.showMessageDialog(this, e.getMessage(),
					"Campo Inválido - " + e.getField(),
					JOptionPane.ERROR_MESSAGE);
		}
	}
}
